﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace ucwp7
{
    public partial class MainPage : PhoneApplicationPage
    {
        //FINISH!
        private CalClass cc = new CalClass();
        private Boolean btn_sign = false;
        private string sign = "";
        private string num1 = null;

        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        // revision 1

        private void bnt_0_Click(object sender, RoutedEventArgs e)
        {
            if (btn_sign)
            {
                txt_Value.Text = "0";
                btn_sign = false;
            }

            if (txt_Value.Text == "0")
                txt_Value.Text = "0";
            else
            {
                txt_Value.Text += "0";
            }
        }

        private void bnt_1_Click(object sender, RoutedEventArgs e)
        {
            if (btn_sign)
            {
                txt_Value.Text = "0";
                btn_sign = false;
            }

            if (txt_Value.Text == "0")
                txt_Value.Text = "1";
            else
            {
                txt_Value.Text += "1";
            }
        }

        private void bnt_2_Click(object sender, RoutedEventArgs e)
        {
            if (btn_sign)
            {
                txt_Value.Text = "0";
                btn_sign = false;
            }

            if (txt_Value.Text == "0")
                txt_Value.Text = "2";
            else
            {
                txt_Value.Text += "2";
            }
        }

        private void bnt_3_Click(object sender, RoutedEventArgs e)
        {
            if (btn_sign)
            {
                txt_Value.Text = "0";
                btn_sign = false;
            }

            if (txt_Value.Text == "0")
                txt_Value.Text = "3";
            else
            {
                txt_Value.Text += "3";
            }
        }

        private void bnt_4_Click(object sender, RoutedEventArgs e)
        {
            if (btn_sign)
            {
                txt_Value.Text = "0";
                btn_sign = false;
            }

            if (txt_Value.Text == "0")
                txt_Value.Text = "4";
            else
            {
                txt_Value.Text += "4";
            }
        }

        private void bnt_5_Click(object sender, RoutedEventArgs e)
        {
            if (btn_sign)
            {
                txt_Value.Text = "0";
                btn_sign = false;
            }

            if (txt_Value.Text == "0")
                txt_Value.Text = "5";
            else
            {
                txt_Value.Text += "5";
            }
        }

        private void bnt_6_Click(object sender, RoutedEventArgs e)
        {
            if (btn_sign)
            {
                txt_Value.Text = "0";
                btn_sign = false;
            }

            if (txt_Value.Text == "0")
                txt_Value.Text = "6";
            else
            {
                txt_Value.Text += "6";
            }
        }

        private void bnt_7_Click(object sender, RoutedEventArgs e)
        {
            if (btn_sign)
            {
                txt_Value.Text = "0";
                btn_sign = false;
            }

            if (txt_Value.Text == "0")
                txt_Value.Text = "7";
            else
            {
                txt_Value.Text += "7";
            }
        }

        private void bnt_8_Click(object sender, RoutedEventArgs e)
        {
            if (btn_sign)
            {
                txt_Value.Text = "0";
                btn_sign = false;
            }

            if (txt_Value.Text == "0")
                txt_Value.Text = "8";
            else
            {
                txt_Value.Text += "8";
            }
        }

        private void bnt_9_Click(object sender, RoutedEventArgs e)
        {
            if (btn_sign)
            {
                txt_Value.Text = "0";
                btn_sign = false;
            }

            if (txt_Value.Text == "0")
                txt_Value.Text = "9";
            else
            {
                txt_Value.Text += "9";
            }
        }

        private void bnt_ea_Click(object sender, RoutedEventArgs e)
        {
            if (txt_Value.Text == "0")
                txt_Value.Text = "0";
            else
            {
                txt_Value.Text = txt_Value.Text.Remove(txt_Value.Text.Length - 1, 1);
                if (txt_Value.Text.Length == 0)
                    txt_Value.Text = "0";
            }
        }

        private void bnt_clear_Click(object sender, RoutedEventArgs e)
        {
            txt_Value.Text = "0";
            num1 = null;
            sign = "";
        }

        // revision 2

        private void bnt_plus_Click(object sender, RoutedEventArgs e)
        {
            sign = "+";
            num1 = txt_Value.Text;
            btn_sign = true;
        }

        private void bnt_minus_Click(object sender, RoutedEventArgs e)
        {

            sign = "-";
            num1 = txt_Value.Text;
            btn_sign = true;
        }

        private void bnt_mul_Click(object sender, RoutedEventArgs e)
        {
            sign = "*";
            num1 = txt_Value.Text;
            btn_sign = true;
        }

        private void bnt_div_Click(object sender, RoutedEventArgs e)
        {
            sign = "/";
            num1 = txt_Value.Text;
            btn_sign = true;
        }

        private void equal_Click(object sender, RoutedEventArgs e)
        {
            if (sign == "+")
            {
                num1 = cc.add_cal(num1, txt_Value.Text);
                sign = "";
            }
            if (sign == "-")
            {
                num1 = cc.minus_cal(num1, txt_Value.Text);
                sign = "";
            }
            if (sign == "*")
            {
                num1 = cc.multi_cal(num1, txt_Value.Text);
                sign = "";
            }
            if (sign == "/")
            {
                num1 = cc.div_cal(num1, txt_Value.Text);
                sign = "";
            }

            txt_Value.Text = num1;
        }
    }
}
